<?php

namespace app\admin\controller;

use app\common\lib\Map;
use think\Cache;
use think\Controller;
use think\Request;

class Common extends Controller
{

    /**
     * 清除缓存
     */
    public function delCache(){
        // 清文件缓存
        $dirs = [ROOT_PATH.'runtime/'];
        @mkdir('runtime',0777,true);
        foreach($dirs as $dir) {
            @unlink($dir);
        }
        // 清理缓存
        Cache::clear();

        $this->success('清除缓存成功！');
    }

    //根据id获取对应的省市区镇
    public function getobjName($id){
        $Area = \app\admin\model\Area::get($id);
        if($Area){
            return $Area->name;
        }else{
            return '所有的';
        }
    }

    public function getaddress(Request $request){
        $param = $request->param();
        $data = $param['data'];
        $res = explode(',',$data);
        $Area = \app\admin\model\Area::where('id','in',[$res[0],$res[1],$res[2]])->select();
        $a1 = '';
        foreach ($Area as $v){
            $a1 .= $v['name'];
        }
        $address = $a1;
        if(isset($res[3])){
            $address .= $res[3];
        }

        return $address;
    }

    /**
     * 根据经纬度获取地址信息
     * @param $lat
     * @param $lng
     * @return \think\response\Json
     */
    public static function getaddressbylatlng($lat,$lng){
        if(empty($lat) || empty($lng)){
            return show(config('status.ERROR_STATUS'),'缺少经纬度','缺少经纬度');
        }
        $res =  Map::geocoder($lat.','.$lng);
        return $res;
    }

    //根据类型获取分类
    public static function getCategory($type){
        $Category = \app\admin\model\Category::where('c_type',$type)->order('sort')->select();
        return $Category;
    }
}